package impl;

import com.javasm.bike.bean.*;
import consts.BikeConstants;
import dao.BikeDao;
import util.DateUtil;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @classname:BikeDaoImol
 * @description:
 * @author: Mr.Z
 * @date: 2020/11/30 21:46
 * @version: 0.1
 * @since:1.8
 */
public class BikeDaoImol implements BikeDao {
    @Override
    public void selectBike(SharedBikeCompany[] sharedBikeCompanies) {
        for (int i = 0, len = sharedBikeCompanies.length; i < len; i++) {
            System.out.println("+----------+---------+--------------------+----------------+");
            System.out.println("| 公司序号 | 公司名称 |   公司单车数量      |  公司借出次数   |");

            System.out.println("|   " + sharedBikeCompanies[i].getBikeCompangId() + "\t|" + sharedBikeCompanies[i].getBikeCompanyName() + "\t|\t\t\t" +
                    "" + sharedBikeCompanies[i].getSum() + "\t\t|\t\t" + sharedBikeCompanies[i].getCount());
            System.out.println("+----------+---------+--------------------+----------------+");
            System.out.println();
            showCompanyBikes(sharedBikeCompanies[i]);
            System.out.println("+----------+---------+--------------------+----------------+");
        }
    }
    //展现某公司的所有单车信息
    public void showCompanyBikes(SharedBikeCompany company) {
        System.out.println("单车序号   单车品牌    单车状态    借出时间");
        for (int i1 = 0, len1 = company.getSum(); i1 < len1; i1++) {
            //判断自行车是否为空
            if (company.getSharedBikes()[i1] == null) {
                break;
            }
            SharedBike sharedBike = company.getSharedBikes()[i1];
            String status = (sharedBike.getStatus() == 1) ? "可借" : "已借出";
            String borrowTime = (sharedBike.getBorrowTime() == null) ? "\t\t_" : sharedBike.getBorrowTime();
            System.out.println("\t" + company.getSharedBikes()[i1].getBid() + "\t|" +
                    " " + company.getSharedBikes()[i1].getBname() + "\t|\t" + status + "\t\t|" + borrowTime);
        }
    }

    @Override
    public void addBike(int addNum, SharedBikeCompany company) {
        //得到要选择增加单车公司的单车数组进行操作
        SharedBike[] bikes = company.getSharedBikes();
        int sum = company.getSum();
//        if ((sum + addNum) > bikes.length) {//扩容数组到10倍多
            bikes = Arrays.copyOf(bikes, (sum + addNum));
            System.out.println("空间不足已经参照配置进行扩容");
//        }
        int lastBikeId = bikes[sum - 1].getBid();

        for (int i = 1; i <= addNum; i++) {
            //获取公司id通过id 来知道创建那个公司的自行车
            int id = company.getBikeCompangId();
            switch (id) {
                case 1:
                    //原来的单车数组序号正好到4，而sum 是5可以充当索引
                    bikes[sum] = new OfoBike((sum + 1), BikeConstants.OFO_BIKE_NAME + (lastBikeId + 1), 1, null);
                    break;
                case 2:
                    bikes[sum] = new MobaiBike((sum + 1), BikeConstants.MOBAI_BIKE_NAME + (lastBikeId + 1), 1, null);
                    break;
                case 3:
                    bikes[sum] = new HaloBike((sum + 1), BikeConstants.HALO_BIKE_NAME + (lastBikeId + 1), 1, null);
                    break;
            }
            sum++;
            lastBikeId++;

        }
        //根据选择的公司添加单车
        company.setSharedBikes(bikes);
//        System.out.println(bikes.length);
        company.setSum(company.getSum() + addNum);
        System.out.println("投放" + addNum + "辆" + company.getBikeCompanyName() + "成功");
        System.out.println("-----------------------");
    }


    @Override
    public void rankBike(SharedBikeCompany[] companies) {
        System.out.println("------>6.排行榜");
        //复制公司数组进行排序对原本的数组没有影响
        SharedBikeCompany[] newCompany = new SharedBikeCompany[companies.length];
        for (int i = 0; i < newCompany.length; i++) {
            newCompany[i] = companies[i];
        }
        int len = newCompany.length;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len - i - 1; j++) {
                if (newCompany[j].getCount() < newCompany[j + 1].getCount()) {
                    SharedBikeCompany temp = newCompany[j + 1];
                    newCompany[j + 1] = newCompany[j];
                    newCompany[j] = temp;
                }
            }
        }
        System.out.println("公司名称" + "\t\t\t" + "借车次数");
        for (int i = 0; i < len; i++) {
            if (newCompany[i].getSharedBikes() != null) {
                System.out.println(newCompany[i].getBikeCompanyName() + "\t\t\t" + newCompany[i].getCount());
            }
        }
    }

    @Override
    public void deleteBike(SharedBikeCompany company, Scanner input) {
        System.out.println(company.getBikeCompanyName() + "共有单车如下");
        showCompanyBikes(company);
        System.out.println("请输入要删除的单车的编号");
        int deleteId = input.nextInt();
        //判断给的Id是否存在
        //存在的前提下判断单车的状态（已借出不可删除）

//        //找出该公司的共享单车数组
        SharedBike[] bikes = company.getSharedBikes();
//        //找到带公司共享单车的总数量
        int sum = company.getSum();
        boolean flag = false;
        for (int i = 0; i < sum; i++) {
            //通过单车数组得到每个单车
            SharedBike bike = bikes[i];
            if (deleteId == bike.getBid()) {
                flag = true;
                if (bike.getStatus() == 0) {
                    System.out.println(bike.getBid() + "共享单车已经借出不能删除");
                    return;
                }
                //不用设置为空直接覆盖掉
//                bikes[i] = null;
                //sum -1;防止下标越界
                for (int j = i; j < sum - 1; j++) {
                    bikes[j] = bikes[j + 1];
                }
                //最后一个数组中的单车为null
                bikes[sum - 1] = null;
                //找到玩删除就不用继续循环了
                break;
            }
        }

        if(!flag) {
            System.out.println(deleteId + "单车不存在录入有问题");
            return;
        }
        System.out.println("删除"+deleteId+"成功");
        //将单车总数减一
        company.setSum(company.getSum()-1);

    }

    //借出功能
    @Override
    public void borrowBike(SharedBikeCompany company, Scanner input) {
        System.out.println(company.getBikeCompanyName() + "共有单车如下");
        showCompanyBikes(company);
        System.out.println("请输入要借出的单车的编号");
        int borrowId = input.nextInt();

        SharedBike[] bikes = company.getSharedBikes();
//        //找到带公司共享单车的总数量
        int sum = company.getSum();
        boolean flag = false;
        for (int i = 0; i < sum; i++) {
            //通过单车数组得到每个单车
            SharedBike bike = bikes[i];
            if (borrowId == bike.getBid()) {
                flag = true;
                if (bike.getStatus() == 0) {
                    System.out.println(bike.getBid() + "共享单车已经借出无法被借");
                    return;
                }
                System.out.println("请输入借出的时间");
                input.nextLine();
                String  borrowTime = input.nextLine();
                bike.setStatus(0);
                bike.setBorrowTime(borrowTime);
                break;
            }
        }

        if(!flag) {
            System.out.println(borrowId + "单车不存在无法借出");
            return;
        }
        System.out.println( "借出id为"+borrowId+"成功");
        company.setCount(company.getCount()+1);

    }

    @Override
    public void returnBike(SharedBikeCompany company, Scanner input) {
        System.out.println(company.getBikeCompanyName() + "共有单车如下");
        showCompanyBikes(company);
        System.out.println("请输入要归还的单车的编号");
        int returnId = input.nextInt();

        SharedBike[] bikes = company.getSharedBikes();
//        //找到带公司共享单车的总数量
        int sum = company.getSum();
        boolean flag = false;
        for (int i = 0; i < sum; i++) {
            //通过单车数组得到每个单车
            SharedBike bike = bikes[i];
            if (returnId == bike.getBid()) {
                flag = true;
                if (bike.getStatus() == 1) {
                    System.out.println(bike.getBid() + "共享单车已经在库无法归还");
                    return;
                }
                System.out.println("请输入归还时间的时间");
                input.nextLine();
                String  returnTime = input.nextLine();
                String boorowTime = bike.getBorrowTime();
                String time = BikeConstants.TIME;
                int money = DateUtil.getDuration(boorowTime, returnTime, time);
                System.out.println("您的借出时间为"+boorowTime);
                System.out.println("用车时间<<"+money+">>需要支付"+money+"元");
                bike.setBorrowTime(null);
                bike.setStatus(1);
                break;
            }
        }

        if(!flag) {
            System.out.println(returnId + "单车不存在无法归还");
            return;
        }

        System.out.println( "归还id为"+returnId+"成功");
    }
}
